package kni.hoid;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class VufindActivity extends ListActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle b = getIntent().getExtras();
		List<Book> result = (List<Book>) b.getSerializable("result");
		if (result.size() == 0) {
			Book message = new Book();
			message.setTitle("Nic nenalezeno");
			result.add(message);
		}
		// Spanned[] array = new Spanned[result.size()];
		String[] array = new String[result.size()];
		int i = 0;
		for (Book book : result) {
			// String html = "<b>";
			String html = "";
			if (book.getAuthor() != null && !book.getAuthor().equals("null")) {
				html += book.getAuthor() + " : ";
			}
			if (book.getTitle() != null) {
				html += book.getTitle();
			}
			if (book.getPublishDate() != null
					&& !book.getPublishDate().equals("null")) {
				html += " (" + book.getPublishDate() + ")";
			}
			// html += "</b>";
			// array[i] = Html.fromHtml(html);
			array[i] = html;
			i++;
		}

		// ArrayAdapter adapter = new ArrayAdapter<Spanned>(this,
		// R.layout.list_item, array);
		// setListAdapter(adapter);
		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, array));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle b = getIntent().getExtras();
				List<Book> result = (List<Book>) b.getSerializable("result");
				String thumbnail = result.get(position).getThumbnailURL();
		
				//v nahledu url
				String url = result.get(position).getOriginalUrl();
//				Toast.makeText(getApplicationContext(), url,
//				          Toast.LENGTH_SHORT).show();
				//po kliknuti prejit na url
				if (thumbnail != null) {
					Intent i = new Intent(Intent.ACTION_VIEW);
					i.setData(Uri.parse(url));
					startActivity(i);
				}
			}

		});
	}
}
